package org.jma.app.ewisdom.entities;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Vector;

import org.jma.lib.soap.utils.XMLSerializable;

import com.thoughtworks.xstream.XStream;

/**
 * Group class stores group information, a Group is a set of users with the same Security attributes 
 * @author Jesus Mari, jesmari@ono.com
 */
public class Group extends HashMap implements Cloneable, Serializable,XMLSerializable {
	private String groupName;
	private String description;
	private Vector users = new Vector();
	/**
	 * @return the groupName
	 */
	public String getGroupName() {
		return groupName;
	}

	/**
	 * @param groupName the groupName to set
	 */
	public void setGroupName(String groupName) {
		this.groupName = groupName;
	}

	/**
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * @param description the description of a group. A string with a text helper description
	 */
	public void setDescription(String description) {
		this.description = description;
	}
	
	/**
	 * Adds an User to a Group. Only stores the userName to avoid crossed references
	 * @param userName a UserName
	 */
	public void addUser(String userName){
		users.add(userName);
	}
	
	/**
	 * Removes a userName from a Group
	 * @param userName
	 */
	
	public void deleteUser(String userName){
		users.remove(userName);
	}
	
	/**
	 * Gets all Users (Vector of userNames) members of this Group
	 * @return
	 */
	public UserVector getUsers(){
		UserVector uv = new UserVector();
		uv.addAll(this.entrySet());
		return uv;
	}
	
	/**
	 * @see XMLSerializable
	 */
    public String serializeToXML() {
    	XStream xstream = new XStream();
    	return xstream.toXML(this);
    }
    
	/**
	 * Overrides the equals method from Object class. Two Groups will be equals if
	 * their groupNames are equal.
	 */    
	public boolean equals(Object content){
		return (this.groupName.equals(((Group)content).groupName));
	}
	
	/**
	 * HashCode calculates its Hashcode
	 * @return Hashcode
	 */
	public int hashCode() {
	    int result = super.hashCode();
	    result = 31 * result + (groupName != null ? groupName.hashCode() : 0);
	    return result;
	}
}
